/*
 AMENDMENTS:
 DATE          NAME        VERSION     DESCRIPTION
--------       -----       -------     -----------
 16/04/2013    MKANG       	1.0.0       1.LogInAction.java : Add to avoid refresh / back action from browser.
*/
package com.mbbmap.app.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mbbmap.app.dao.SysLogDao;
import com.mbbmap.app.home.SecUsersHome;
import com.mbbmap.app.home.SysLogHome;
import com.mbbmap.security.action.CommonDispatchAction;
import com.mbbmap.security.dao.SecUserDao;
import com.mbbmap.util.Constants;

/**
 * Initiate logout action
 */
public final class LogOutAction extends CommonDispatchAction {

  public ActionForward init(ActionMapping mapping,
    ActionForm form, HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    String PROG_ID="LogOutAction.init";
    SysLogDao sysLogDao = new SysLogDao();
    HttpSession session = request.getSession(false);

    if (session !=null) {
	    SecUserDao secUser = (SecUserDao) session.getAttribute(Constants.SEC_USER_OBJ);
	    String merchant = (String) session.getAttribute(Constants.MERCHANT_KEY);

	    if (secUser == null){
			if (DEBUG) System.out.println("LogOutAction.init secUser null");
			return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
		}else{
			// Activity log
			sysLogDao.setActivityCode(Constants.ACT_CODE_LOGIN);
			sysLogDao.setAcctID(Constants.APP_MERCHANT);
			sysLogDao.setActivityName(PROG_ID);
			sysLogDao.setStatus(Constants.ACT_STATUS_SUCCESS);

			sysLogDao.setUserID(secUser.getLogonId());
			sysLogDao.setRole(secUser.getGroupCode());

			boolean logoutSuccess = SecUsersHome.getInstance().logout(merchant, secUser.getLogonId());
			if (!logoutSuccess) {
				sysLogDao.setStatus(Constants.ACT_STATUS_FAIL_GENERAL);
				sysLogDao.setUserField1("SecUsersHome.logout() failed");
			}

			SysLogHome.getInstance().insertSysLog(sysLogDao);
			session.invalidate();
			request.setAttribute("LogOut", "LogOut");

			if (DEBUG) System.out.println("LogOutAction.init");
			return mapping.findForward(Constants.KEY_ACTION_SUCCESS);
		}

    } else {
        return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
    }

  }
  
  public ActionForward leave(ActionMapping mapping,
		    ActionForm form, HttpServletRequest request,
		    HttpServletResponse response) throws Exception {

		    String PROG_ID="LogOutAction.leave";
		    SysLogDao sysLogDao = new SysLogDao();
		    HttpSession session = request.getSession(false);

		    if (session !=null) {
			    SecUserDao secUser = (SecUserDao) session.getAttribute(Constants.SEC_USER_OBJ);
			    String merchant = (String) session.getAttribute(Constants.MERCHANT_KEY);

			    if (secUser == null){
					if (DEBUG) System.out.println("LogOutAction.leave secUser null");
					return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
				}else{
					// Activity log
					sysLogDao.setActivityCode(Constants.ACT_CODE_LOGIN);
					sysLogDao.setAcctID(Constants.APP_MERCHANT);
					sysLogDao.setActivityName(PROG_ID);
					sysLogDao.setStatus(Constants.ACT_STATUS_SUCCESS);

					sysLogDao.setUserID(secUser.getLogonId());
					sysLogDao.setRole(secUser.getGroupCode());

					boolean logoutSuccess = SecUsersHome.getInstance().logout(merchant, secUser.getLogonId());
					if (!logoutSuccess) {
						sysLogDao.setStatus(Constants.ACT_STATUS_FAIL_GENERAL);
						sysLogDao.setUserField1("SecUsersHome.logout() failed");
					}

					SysLogHome.getInstance().insertSysLog(sysLogDao);
					session.invalidate();
					request.setAttribute("LogOut", "LogOut");

					if (DEBUG) System.out.println("LogOutAction.leave");
					return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
				}

		    } else {
		        return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
		    }

		  }


  public void refresh(ActionMapping mapping,
    ActionForm form, HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    String PROG_ID="LogOutAction.refresh";
    HttpSession session = request.getSession(false);
    if (DEBUG) System.out.println("LogOutAction.refresh");
    //return mapping.findForward(Constants.KEY_ACTION_SUCCESS);
  }

}
